#include "encoderoption.h"
#include <QStringList>

EncoderOption::EncoderOption(QSettings & s, const QString &id) {
    m_settings = &s;
    setParent(&s);
    setId(id);
    loadFromSettings();
}

EncoderOption::~EncoderOption() {
    writeToSettings();
}

void EncoderOption::setValue(const QString &key, const QVariant & val) {
    m_values[key] = val;
    m_settings->setValue(key,val);
    emit valueChanged(key,val);
}

QVariant EncoderOption::value(const QString &key, const QVariant &v) {
    if (!m_values.contains(key)) return v;
    else return m_values[key];
}

bool EncoderOption::contains(const QString &key) const {
    return m_values.contains(key);
}

EncoderOption &EncoderOption::operator =(const EncoderOption & op) {
    EncoderOption *ret = new EncoderOption (*op.m_settings,op.id());

    ret->m_values = op.m_values;

    return *ret;
}

void EncoderOption::writeToSettings() {
    QHash<QString, QVariant>::const_iterator i = m_values.constBegin();

    while( i != m_values.constEnd()) {
	m_settings->setValue(i.key(),i.value());
    }
}

void EncoderOption::loadFromSettings() {
    m_values.clear();
    QStringList keys = m_settings->allKeys();

    foreach (QString key, keys) {
	m_values[key] = m_settings->value(key);
    }
}

void EncoderOption::setName(const QString &name) {
    setValue("name", name);
}

QString EncoderOption::name() const {
    return m_values.value("name", "").toString();
}
